package controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bo.ReasonEstimateBo;
import bo.StacTraceBo;
import vo.CauseVo;
import vo.StackTraceVo;
import dao.hibernate.CauseVoDAO;
import dao.hibernate.StackTraceVoDAO;

import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StackTraceEventStatServlet extends HttpServlet {
	private static Logger LOGGER = LoggerFactory.getLogger(StackTraceEventStatServlet.class);
	
	/**
	 * Constructor of the object.
	 */
	public StackTraceEventStatServlet() {
		super();
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		LOGGER.debug("doGet"  );
		String keyCauseUid = request.getParameter("keyCauseUid"); 
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		LOGGER.debug("keyCauseUid: {}",keyCauseUid  );
		RequestDispatcher dispatcher = request.getRequestDispatcher("ipDisplay.jsp");
	    dispatcher.forward(request, response); 
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(final HttpServletRequest request, final HttpServletResponse response)
			throws ServletException, IOException {
		LOGGER.debug("doPost"  );
		String keyCauseUid = request.getParameter("keyCauseUid"); 
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		LOGGER.debug("keyCauseUid: {}",keyCauseUid  );
		StacTraceBo bo = new StacTraceBo();
		final	Map<String, Integer> map = bo.getStaticsData(NumberUtils.toInt(keyCauseUid),startDate,endDate);
		request.setAttribute("stackTraceStatMap", map);
		request.setAttribute("keyCauseUid", keyCauseUid);
		request.setAttribute("startDate", startDate);
		request.setAttribute("endDate", endDate);
		RequestDispatcher dispatcher = request.getRequestDispatcher("ipDisplay.jsp");
	    dispatcher.forward(request, response); 
		
	}

}
